package com.fcscs.cservice.client.main.configuration.inroomdining;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.fcscs.core.common.authority.UserAuthority;
import com.fcscs.core.domain.IdNameModel;
import com.fcscs.core.domain.Property;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.SessionContext;
import com.fcscs.core.gwt.common.ui.FcsFields.EasyClearCombobox;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.FcsPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.model.configuration.inroomdining.ChargeCategory;
import com.fcscs.cservice.client.common.model.configuration.inroomdining.ChargeCategoryModelData;
import com.fcscs.cservice.client.common.model.configuration.reservation.PropertyIdNameModelData;
import com.google.gwt.user.client.ui.Widget;

public class ChargeCategoryPanel extends FcsPanel {

	private VerticalPanel vp;
	private EasyClearCombobox<PropertyIdNameModelData> propCombox;
	private ListStore<PropertyIdNameModelData> propListStore;
	private ChargeCategoryGrid grid;
	private FormButton addBtn;
	private FormButton setComConfigBtn;
	private ChargeCategoryCommonConfigDialog chargeCommDialog;

	private ChargeCategoryDialog chargeCategoryDialog;
	private List<IdNameModel<Property>> propGroupIdNames;

	public ChargeCategoryPanel(String id) {
		super(id);

	}

	@Override
	public void init() {
		if (null == vp) {

			vp = new VerticalPanel();
			vp.setId("ChargeCategoryPanel_vp");
			vp.setSpacing(5);

			HorizontalPanel hp = new HorizontalPanel();
			hp.setId("ChargeCategoryPanel_hp");
			hp.setSpacing(5);
			vp.add(hp);

			Label propLabel = new Label(CoreConstants.CORE_CONSTANTS.property()
					+ CoreConstants.REQURIED_FIELD_LABEL);
			propLabel.setId("ChargeCategoryPanel_propLabel");
			propLabel.setWidth(150);

			propListStore = new ListStore<PropertyIdNameModelData>();
			propCombox = new EasyClearCombobox<PropertyIdNameModelData>();
			propCombox.setId("ChargeCategoryPanel_propCombox");
			propCombox.setWidth(150);
			propCombox.setDisplayField(CoreConstants.FULLTNAME);
			propCombox.setAllowBlank(false);
			propCombox.setForceSelection(true);
			propCombox.setTriggerAction(TriggerAction.ALL);
			propCombox.setStore(propListStore);
			propCombox.addSelectionChangedListener(new SelectionChangedListener<PropertyIdNameModelData>() {

				@Override
				public void selectionChanged(SelectionChangedEvent<PropertyIdNameModelData> se) {
					PropertyIdNameModelData pinmmd = propCombox.getValue();
					if (pinmmd != null) {
						newSearch();
					}
				}
			});

			hp.add(propLabel);
			hp.add(propCombox);

			setComConfigBtn = new FormButton(CServiceConstants.CSERVICE_CONSTANTS.setCommonConfig());
			setComConfigBtn.setId("ChargeCategoryPanel_setComConfigBtn");
			setComConfigBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {

				@Override
				public void componentSelected(ButtonEvent ce) {
					chargeCommDialog = ChargeCategoryCommonConfigDialog.getInstance();
					chargeCommDialog.show();
				}
			});
			hp.add(setComConfigBtn);

			grid = new ChargeCategoryGrid(CServiceConstants.CSERVICE_CONSTANTS.chargeCategory(), null);
			grid.setId("ChargeCategoryPanel_grid");
			vp.add(grid);

			addBtn = new FormButton(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.add(),
					CServiceConstants.CSERVICE_CONSTANTS.chargeCategory()));
			addBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
				@Override
				public void componentSelected(ButtonEvent ce) {
					chargeCategoryDialog = ChargeCategoryDialog.getInstance(true);
					chargeCategoryDialog.show();
				}
			});

			vp.add(addBtn);

			add(vp);
			onShow();
		}

	}

	@Override
	public UserAuthority getRequiredAuthority() {
		return null;
	}

	@Override
	protected Widget getViewPanel() {
		return null;
	}

	@Override
	public void onShow() {
		loadProperty();
	}

	@Override
	public boolean onHide() {
		return true;
	}

	public void newSearch() {
		grid.removeAllModels();
		List<ChargeCategoryModelData> chargeCategoryList = new ArrayList<ChargeCategoryModelData>();
		ChargeCategory model1 = new ChargeCategory();
		model1.setChargeCategory("category1");
		model1.setDeliveryCharge("delivery1");
		model1.setSalesTax("salesTax1");
		model1.setServiceCharge("serviceCharge1");
		model1.setVAT("VAT1");
		chargeCategoryList.add(new ChargeCategoryModelData(model1));
		grid.addModels(chargeCategoryList);
	}

	private void loadProperty() {
		propGroupIdNames = SessionContext.getInstance().getPropertyGroup();
		propListStore.removeAll();
		if (0 != propGroupIdNames.size()) {
			int propIndex = 0;
			for (IdNameModel<Property> inm : propGroupIdNames) {
				propListStore.add(new PropertyIdNameModelData(inm));
				if (propIndex == 0) {
					if (SessionContext.getInstance().getPropertyId() == inm.getId()) {
						propIndex = propListStore.getCount() - 1;
					}
				}
			}
			propCombox.setValue(propListStore.getAt(propIndex));
		} else {
			IdNameModel<Property> tempINM = new IdNameModel<Property>(SessionContext.getInstance()
					.getPropertyId(), SessionContext.getInstance().getPropertyName());
			propListStore.add(new PropertyIdNameModelData(tempINM));
			propCombox.setValue(propListStore.getAt(0));
		}
	}
}
